﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Agency_KaiBiaoJL : System.Web.UI.Page
{
    private const int GridColumnIndexOfbidid = 0;
    private const int GridColumnIndexOfBeiZhu1 = 14;
    private const int GridColumnIndexOfBeiZhu2 = 15;

    public string _strProjectId = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            _strProjectId = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["projectid"]));
            if (!IsPostBack)
            {
                Db db = new Db();
                string strSql = "select distinct AccountId_TB, salername from bidgoods_view where projectid=" + _strProjectId + " order by salername";
                System.Data.IDataReader dr = db.DataReader(strSql);
                Company.DataSource = dr;
                Company.DataTextField = "salername";
                Company.DataValueField = "AccountId_TB";
                Company.DataBind();
                Company.Items.Insert(0, new ListItem("全部", "-1"));
                dr.Close();
                dr.Dispose();
                strSql = "select 1 as BJ  from  sysobjects  where  id = object_id('KaiBiaoJL" + _strProjectId + "')  and   type = 'U'";
                dr = db.DataReader(strSql);
                int _intBJ = 0;
                if (dr.Read())
                {
                    _intBJ = SysFun.ToInt(dr["BJ"]);
                }
                dr.Close();
                dr.Dispose();
                db.Close();
                ProductCode.Text = "";
                ProductName.Text = "";
                Outlookc.Text = "";
                Company.SelectedValue = "";
                if (_intBJ == 0)
                {
                    Response.Redirect("../HomePage/Default.aspx");
                }
            }
            if (Request.Form["SubSave"] == "保存备注信息")
            {
                //string Bidid = "";
                //String BeiZhu = "", strSql;
                //Bidid = SysFun.ToTrim(Request.Form["Bidid"]);
                //Db db = new Db();
                //if (Bidid != "")
                //{
                //    string[] KBBID = Bidid.Length == 0 ? new string[] { "" } : Bidid.Substring(0).Split(new char[] { ',' });
                //    int nNumber = 0;
                //    strSql = "";
                //    for (int i = 0; i < KBBID.Length; i++)
                //    {
                //        nNumber++;
                //        BeiZhu = Request.Form["beizhu" + KBBID[i]];
                //        strSql += "@" + "Update KaiBiaoJL" + _strProjectId + " Set beizhu='" + BeiZhu + "' Where Bidid=" + KBBID[i].ToString();
                //    }
                //    if ((strSql.Length > 0) && (nNumber == KBBID.Length))
                //    {
                //        db.ExecuteTransaction(strSql.Substring(1).Split(new char[] { '@' }), 30);
                //        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                //        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "", "KaiBiaoJL" + _strProjectId, strSql, "../Agency/KaiBiaoJL.aspx");

                //        Response.Write("<script>alert('数据保存成功！');</script>");
                //    }
                //}
                //db.Close();
                Int64 _intBidid = 0;
                string strSql = "";
                foreach (GridViewRow row in GridView1.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        _intBidid = SysFun.ToLong(row.Cells[GridColumnIndexOfbidid].Text);
                        string _strBeiZhu = SysFun.ToTrim(Request.Form["txtBeiZhu" + _intBidid.ToString()]).Replace("&nbsp;", "");
                        strSql += "@" + "Update KaiBiaoJL" + _strProjectId + " Set beizhu='" + _strBeiZhu + "' Where Bidid=" + _intBidid.ToString();


                    }
                }
                if (strSql.Length > 0)
                {
                    Db db = new Db();
                    db.ExecuteTransaction(strSql.Substring(1).Split(new char[] { '@' }), 30);
                    HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "KaiBiaoJL" + _strProjectId, strSql, "../Agency/KaiBiaoJL.aspx");

                    Response.Write("<script>alert('数据保存成功！');</script>");
                    db.Close();
                }


            }
            showGridView();
            if (Request.Form["subPrint"] == "打  印")
            {
                PrintKB();
            }
        }
    }
    public void showGridView()
    {
        int i = 0;
        string strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "KaiBiaoJL" + _strProjectId;                                               //要查询的表明
        dbPage.Key = "bidid";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "bidid,goodsid,ProductName,Outlookc,Factor,Medicinemodel,Unit,ProductCode,CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS QualityLevels,UserCode_TB,CompanyName_TB,UserCode_SC,CompanyName_SC,goodsName,SPPrice,SMPrice,LimitPrice,beizhu";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        if (Request.Form["subPrint"] == "打  印")
        { 
            dbPage.PageSize = 1; 
        }
        else
        { 
            dbPage.PageSize = 20; 
        }
        dbPage.OrderBy = "productname,medicinemodel,outlookc,factor,spprice ,bidid";//排序字段

        System.Data.IDataReader dr = dbPage.ExecuteReader();
        //System.Text.StringBuilder strHtml = new System.Text.StringBuilder();
        //strHtml.Append("<table width='98%' border='0' align='center' cellpadding='2' cellspacing='1' class='table_table'>");
        //strHtml.Append("<tr>");
        //strHtml.Append("<td class='table_title' >药品流水号</td>");
        //strHtml.Append("<td class='table_title' >品目号</td>");
        //strHtml.Append("<td class='table_title' >通用名</td>");
        //strHtml.Append("<td class='table_title' >规格</td>");
        //strHtml.Append("<td class='table_title' >剂型</td>");
        //strHtml.Append("<td class='table_title' >转换系数</td>");
        //strHtml.Append("<td class='table_title' >生产企业名称</td>");
        //strHtml.Append("<td class='table_title' >生产企业注册号</td>");
        //strHtml.Append("<td class='table_title' >包装报价</td>");
        //strHtml.Append("<td class='table_title' >制剂报价</td>");
        //strHtml.Append("<td class='table_title' >限价</td>");
        //strHtml.Append("<td class='table_title' >投标人注册号</td>");
        //strHtml.Append("<td class='table_title' >投标人</td>");
        //strHtml.Append("<td class='table_title' >备注</td>");
        //strHtml.Append("</tr>");


        //while (dr.Read())
        //{
        //    if (i == 0)
        //    {
        //        strHtml.Append("<tr class='table_td'>");
        //        i++;
        //    }
        //    else
        //    {
        //        strHtml.Append("<tr class='table_td2'>");
        //        i--;
        //    }
        //    strHtml.Append("<td><Input Name='BidId' type='Hidden' Value='" + SysFun.ToTrim(dr["Bidid"].ToString()) + "'>" + dr["goodsid"].ToString() + "</td>");
        //    strHtml.Append("<td>" + dr["ProductCode"].ToString() + "</td>");
        //    strHtml.Append("<td>" + dr["ProductName"].ToString() + "</td>");
        //    strHtml.Append("<td>" + dr["Outlookc"].ToString() + "</td>");
        //    strHtml.Append("<td>" + dr["Medicinemodel"].ToString() + "</td>");
        //    strHtml.Append("<td>" + dr["Factor"].ToString() + "</td>");
        //    strHtml.Append("<td>" + dr["CompanyName_SC"].ToString() + "</td>");
        //    strHtml.Append("<td>" + dr["UserCode_SC"].ToString() + "</td>");
        //    strHtml.Append("<td>" + SysFun.ToDec(dr["SPPrice"].ToString()).ToString("#.00") + "</td>");
        //    strHtml.Append("<td>" + SysFun.ToDec(dr["SMPrice"].ToString()).ToString("#.0000") + "</td>");
        //    strHtml.Append("<td>" + SysFun.ToDec(dr["LimitPrice"].ToString()).ToString("#.00") + "</td>");
        //    strHtml.Append("<td>" + dr["UserCode_TB"].ToString() + "</td>");
        //    strHtml.Append("<td>" + dr["CompanyName_TB"].ToString() + "</td>");
        //    //strHtml.Append("<td><Input name='beizhu"+ dr["Bidid"].ToString() +"' type='text' value='"+ SysFun.ToTrim(dr["beizhu"].ToString()) +"' /></td>");
        //    strHtml.Append("<td><textarea  name='beizhu" + dr["Bidid"].ToString() + "' col='10' rows='5' />" + SysFun.ToTrim(dr["beizhu"].ToString()) + "</textarea></td>");
        //    strHtml.Append("</tr>");
        //}
        //strHtml.Append("</table>");
        //Label1.Text = strHtml.ToString();

        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();

        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键



    }

    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp, _GoodsId;

        strtemp = " projectid=" + _strProjectId + " and";

        //流水号
        _GoodsId = SysFun.ToTrim(txtGoodsId.Text);
        if (_GoodsId != "")
        {
            strtemp += " GoodsId like '%" + _GoodsId + "%' and";
        }

        //生产企业名称
        string _strCompanyName_SC = SysFun.ToTrim(txtSC.Text);
        if (_strCompanyName_SC != "")
        {
            strtemp += " CompanyName_SC like '%" + _strCompanyName_SC + "%' and";
        }

        //品目号
        string _strProductCode = ProductCode.Text.ToString().Trim();
        if (_strProductCode != "")
        {
            strtemp += " ProductCode like '%" + _strProductCode + "%' and";
        }

        //药品名称
        string _strProductName = ProductName.Text.ToString().Trim();
        if (_strProductName != "")
        {
            strtemp += " (ProductName like '%" + _strProductName + "%'or SpellingCode like '%" + _strProductName + "%') and";
        }

        //规格
        string _strOutlookc = Outlookc.Text.ToString().Trim();
        if (_strOutlookc != "")
        {
            strtemp += " Outlookc like '%" + _strOutlookc + "%' and";
        }

        //投标人名称
        string _strCompanyName_TB = SysFun.ToTrim(Company.SelectedValue);
        if (_strCompanyName_TB != "-1")
        {
            strtemp += " AccountId_TB=" + _strCompanyName_TB + " and";
        }

        //投标人注册号
        string _strUserCode_TB = txtCompanyUserCode.Text.ToString().Trim();
        if (_strUserCode_TB != "")
        {
            strtemp += " UserCode_TB='" + _strUserCode_TB + "' and";
        }

        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }

        return strtemp;

    }



    protected void Button5_Click(object sender, EventArgs e)
    {

    }
  
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (Request.Form["subPrint"] == "打  印")
            {
                e.Row.Cells[GridColumnIndexOfBeiZhu1].Text = e.Row.Cells[GridColumnIndexOfBeiZhu2].Text.Trim().Replace("&nbsp;", "");
            }
            else
            {
                e.Row.Cells[GridColumnIndexOfBeiZhu1].Text = "<textarea  style='height:50;' name='txtbeizhu" + e.Row.Cells[GridColumnIndexOfbidid].Text.Trim() + "' >" + e.Row.Cells[GridColumnIndexOfBeiZhu2].Text.Trim().Replace("&nbsp;", "") + "</textarea>";
            }
        }
        e.Row.Cells[GridColumnIndexOfbidid].Visible = false;
        e.Row.Cells[GridColumnIndexOfBeiZhu2].Visible = false;

    }
    protected void PrintKB()
    {

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        // 添加头信息，为"文件下载/另存为"对话框指定默认文件名 
        Response.AddHeader("Content-Disposition", "attachment; filename=11.xls");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType = "application/ms-excel";
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        this.GridView1.RenderControl(oHtmlTextWriter);

        // 把文件流发送到客户端 
        Response.Write(oStringWriter.ToString());
        // 停止页面的执行

        Response.End();


    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //OverRide　为了使导出成Excel可行！
    }
}
